package com.siyuanren.gcocp.service.impl;


import cn.hutool.core.date.DateUtil;
import com.siyuanren.gcocp.dto.wx.MsgDTO;
import com.siyuanren.gcocp.entity.WxTemplateMsg;
import com.siyuanren.gcocp.service.MsgService;
import com.siyuanren.gcocp.service.WxUserService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;

/**
 * @author guwr
 */
@Slf4j
@Service
public class MsgServiceImpl implements MsgService {

    @Autowired
    private WxMpService wxMpService;
    @Autowired
    private WxUserService userService;

    @Override
    public void sendTemplateMessage(String userOpenId, WxTemplateMsg templateMsg) {
        try {
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(userOpenId)
                    .templateId(templateMsg.getTemplateId())
                    .url(templateMsg.getUrl())
                    .build();
            String nowDate = DateUtil.formatDate(new Date());
            String keyword1 = NOW_DATE.equals(templateMsg.getKeyword1()) ? nowDate : templateMsg.getKeyword1();
            String keyword2 = NOW_DATE.equals(templateMsg.getKeyword2()) ? nowDate : templateMsg.getKeyword2();
            String keyword3 = NOW_DATE.equals(templateMsg.getKeyword3()) ? nowDate : templateMsg.getKeyword3();


            templateMessage.addData(new WxMpTemplateData("first", templateMsg.getFirst(), templateMsg.getColorFirst()));
            templateMessage.addData(new WxMpTemplateData("keyword1", keyword1, templateMsg.getColor1()));
            templateMessage.addData(new WxMpTemplateData("keyword2", keyword2, templateMsg.getColor2()));
            templateMessage.addData(new WxMpTemplateData("keyword3", keyword3, templateMsg.getColor3()));
            templateMessage.addData(new WxMpTemplateData("remark", templateMsg.getRemark(), templateMsg.getColorRemark()));
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            log.info("{}_模版发送失败", userOpenId);
            e.printStackTrace();
        }
    }

    @Override
    public void sendCustomerServiceMessage(MsgDTO msgDTO) {
        String openid = msgDTO.getOpenid();
        WxMpKefuMessage message = new WxMpKefuMessage();
        message.setMsgType(WxConsts.KefuMsgType.TEXT);
        message.setToUser(openid);
        message.setContent(msgDTO.getContent());
        message.setDescription(msgDTO.getDesc());
        try {
            wxMpService.getKefuService().sendKefuMessage(message);
        } catch (WxErrorException e) {
            log.info("{}_ERROR", msgDTO.getDesc());
            log.error(e.getMessage());
            e.printStackTrace();
        }
    }

    @Override
    public void sendFileToUser(String userOpenId, File file) throws WxErrorException {
        WxMediaUploadResult wxMediaUploadResult = wxMpService.getMaterialService().mediaUpload(WxConsts.KefuMsgType.IMAGE, file);
        String mediaId = wxMediaUploadResult.getMediaId();
        WxMpKefuMessage reply = new WxMpKefuMessage();
        reply.setToUser(userOpenId);
        reply.setMsgType(WxConsts.KefuMsgType.IMAGE);
        reply.setMediaId(mediaId);
        //再发送
        wxMpService.getKefuService().sendKefuMessage(reply);
    }
}
